package com.syezon.wifikey.pwd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.wifi.info.WifiInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/12.
 */

public class AddPwdAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_TITLE = 1;

    private Context mContext;
    private List<WifiInfo> mData = new ArrayList<>();
    private AddPwdListener mAddListener;

    public AddPwdAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<WifiInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            WifiInfo wifiInfo = new WifiInfo();
            mData.add(0, wifiInfo);
            notifyDataSetChanged();
        }
    }

    public void setListener(AddPwdListener addPwdListener) {
        if (addPwdListener != null) {
            mAddListener = addPwdListener;
        }
    }

    public List<WifiInfo> getData() {
        return mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            return new AddWifiTitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_addwifi_title, parent, false));
        } else {
            return new AddWifiPwdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_addwifi, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (position != 0) {
            final WifiInfo wifiInfo = mData.get(position);
            if (holder instanceof AddWifiPwdViewHolder) {
                final AddWifiPwdViewHolder viewHolder = (AddWifiPwdViewHolder) holder;
                viewHolder.tvWifiName.setText(wifiInfo.getSsid());
                viewHolder.etWifiPwd.setText("");
                viewHolder.tvAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mAddListener != null) {
                            mAddListener.addPwd(wifiInfo, viewHolder.etWifiPwd.getText().toString());
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TITLE;
        } else {
            return TYPE_ITEM;
        }
    }

    public static class AddWifiPwdViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_wifi_icon)
        ImageView ivWifiIcon;
        @BindView(R.id.tv_wifiname)
        TextView tvWifiName;
        @BindView(R.id.et_wifipassword)
        TextView etWifiPwd;
        @BindView(R.id.tv_add)
        TextView tvAdd;

        public AddWifiPwdViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class AddWifiTitleViewHolder extends RecyclerView.ViewHolder {

        public AddWifiTitleViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static interface AddPwdListener {
        void addPwd(WifiInfo wifiInfo, String pwd);
    }
}
